package com.tanhua.backstage.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.backstage.service.LogService;
import com.tanhua.backstage.service.VideoService;
import com.tanhua.backstage.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
@RequestMapping("manage")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private LogService logService;


    /**
     * 控制层实现功能:视频翻页
     * @param pageSize
     * @param page
     * @param sortProp
     * @param sortOrder
     * @param uid
     * @return
     */
    @GetMapping("videos")
    public ResponseEntity<PageResult> getVideoInfo(
            @RequestParam("pagesize") Integer pageSize,
            @RequestParam("page") Integer page,
            @RequestParam("sortProp") String sortProp,
            @RequestParam("sortOrder") String sortOrder,
            @RequestParam("uid") Long uid) {
        try {
            //获取业务层返回的结果
            PageResult pageResult = videoService.getVideoInfo(page, pageSize, sortProp, sortOrder, uid);
            if (ObjectUtil.isNotEmpty(pageResult)) {
                //返回结果给前端对象
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok(null);
    }


    /**
     * 日志翻页
     * @param page
     * @param pageSize
     * @param sortProp  排序字段   (只按时间排序)
     * @param sortOrder  排序方式
     * @param type
     * @param uid
     * @return
     */
    @GetMapping("logs")
    public ResponseEntity<PageResult> getLogRecordPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                       @RequestParam(value = "sortProp", required = false) String sortProp,
                                                       @RequestParam(value = "sortOrder", required = false) String sortOrder,
                                                       @RequestParam(value = "type", required = false) Integer type,
                                                       @RequestParam(value = "uid", required = false) Long uid) {

        //查询日志列表
        PageResult pageResult = logService.getLogRecordPage(page, pageSize, sortProp, sortOrder, type, uid);

        if (ObjectUtil.isNotEmpty(pageResult)) {
            return ResponseEntity.ok(pageResult);
        }
        return ResponseEntity.status(500).build();
    }

}
